package cn.cmdr.controller;

import cn.cmdr.pojo.Journal;
import cn.cmdr.pojo.ReturnReason;
import cn.cmdr.pojo.Staff;
import cn.cmdr.service.JournalService;
import cn.cmdr.service.ReturnReasonService;
import cn.cmdr.service.ReturnsService;
import cn.cmdr.vo.ResultVo;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;;
import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/ord/reason")
public class ReturnReasonController {
    @Resource
    private ReturnReasonService returnReasonService;
    @Resource
    private ReturnsService returnsService;
    @Resource
    private JournalService journalService;

    //分页查询退货原因
    @PostMapping("/list/{pageSize}/{pageNo}")
    public ResultVo getlist(@PathVariable Integer pageSize, @PathVariable Integer pageNo, @RequestBody ReturnReason returnReason){
        return ResultVo.sucess(returnReasonService.getPageReturnReasonList(pageNo,pageSize,returnReason));
    }
    @PostMapping("/reasonList")
    public ResultVo getReasonlist(){
        List<ReturnReason> list = returnReasonService.getReturnReasonList();
        return ResultVo.sucess(list);
    }
    //修改退货原因是否可用
    @PostMapping("/update")
    @RequiresPermissions("reason:update")
    public ResultVo getupdate(@RequestBody ReturnReason returnReason){
        try {
            if(returnReason.getReturnState().equals("0")||returnReason.getReturnState().equals("false")){
                returnReason.setReturnState("1");
            }else if(returnReason.getReturnState().equals("1")||returnReason.getReturnState().equals("true")){
                returnReason.setReturnState("0");
            }
            returnReasonService.getReturnReasonUpdate(returnReason);
            Journal journal = new Journal();
            journal.setStaffId(((Staff) SecurityUtils.getSubject().getPrincipal()).getId());
            journal.setUpDate(new Date());
            String sf="";
            if(returnReason.getReturnState().equals("0")){
                sf="否";
            }else if(returnReason.getReturnState().equals("1")){
                sf="是";
            }
            journal.setUpContent("修改退货原因是否可用为:"+sf);
            journal.setMenuId(6);
            journalService.add(journal);
            return ResultVo.sucess("状态修改成功");
        }catch (Exception e){
            return ResultVo.fail("状态修改失败",e);  //包含了打印错误日志
        }
    }

    //添加退货原因
    @PostMapping("/add")
    @RequiresPermissions("reason:add")
    public ResultVo getadd(@RequestBody ReturnReason returnReason){
        try {
            returnReason.setReturnTime(new Date());
            returnReasonService.getReturnReasonAdd(returnReason);
            Journal journal = new Journal();
            journal.setStaffId(((Staff) SecurityUtils.getSubject().getPrincipal()).getId());
            journal.setUpDate(new Date());
            journal.setUpContent("添加退货原因:"+returnReason.getReturnName());
            journal.setMenuId(6);
            journalService.add(journal);
            return ResultVo.sucess("添加原因成功");
        }catch (Exception e){
            return ResultVo.fail("添加原因失败",e);  //包含了打印错误日志
        }
    }

    //删除退货原因
    @PostMapping("/delete")
    @RequiresPermissions("reason:del")
    public ResultVo getdelete(String id){
        try {
            Integer i = returnsService.shifou(Integer.parseInt(id));
            if(i!=0){
                return ResultVo.fail("该原因在使用，不能删除");
            }{

                ReturnReason returnReason = returnReasonService.getById(Integer.parseInt(id));
                Journal journal = new Journal();
                journal.setStaffId(((Staff) SecurityUtils.getSubject().getPrincipal()).getId());
                journal.setUpDate(new Date());
                journal.setUpContent("删除退货原因:"+returnReason.getReturnName());
                journal.setMenuId(6);
                returnReasonService.getReturnReasonDelete(Integer.parseInt(id));
                journalService.add(journal);
            }
            return ResultVo.sucess("删除原因成功");
        }catch (Exception e){
            return ResultVo.fail("删除原因失败",e);  //包含了打印错误日志
        }
    }

    //修改退货原因
    @PostMapping("/update2")
    @RequiresPermissions("reason:update")
    public ResultVo getupdate2(@RequestBody ReturnReason returnReason){
        try {
            returnReasonService.getReturnReasonUpdate(returnReason);
            Journal journal = new Journal();
            journal.setStaffId(((Staff) SecurityUtils.getSubject().getPrincipal()).getId());
            journal.setUpDate(new Date());
            journal.setUpContent("修改退货原因:"+returnReason.getReturnName());
            journal.setMenuId(6);
            journalService.add(journal);
            return ResultVo.sucess("修改原因成功");
        }catch (Exception e){
            return ResultVo.fail("修改原因失败",e);  //包含了打印错误日志
        }
    }

    @PostMapping("list")
    public List<ReturnReason> list(){
        Integer state=1;
        return returnReasonService.getList(state);
    }
}
